-- sunflower
-- created by panyl
-- 冈布奥事件：黑暗龙雕像

-- 购买商场物品
EventMgr.register("sunflower", event.MAKE_SUNSHINE, function(para)
    local arr = SlimeEventM.getIdsByRule("sunflower");
    for index = 1, #arr do
        if SlimeEventM.triggerEvent(arr[index], para) then
            -- 只要触发一个就行了
            break;
        end
    end
end);

return {
    checkEvent = function(eventId, para)
        -- 太阳花
        local dbase = SlimeEventM.query(eventId, "dbase") or {};
        local classId = dbase["sunshine"];
        if not classId then
            return false;
        end

        local amount = SlimeEventM.query(eventId, "condition");
        if DungeonServiceM.getItemStat(classId) < amount then
            return false;
        end

        if dbase["slime"] and PetM.getMyPet(dbase["slime"]) then
            -- 奖励冈布奥的，不能已经有冈布奥
            return false;
        end

        if dbase["slime_scrap"] and not PetM.getMyPet(dbase["slime_scrap"]) then
            -- 奖励冈布奥碎片的，需要先有冈布奥
            return false;
        end

        return true;
    end,

    triggerEvent = function(eventId, para)
        local pos = para["pos"];
        local grid = DungeonM.getGridByPos(pos);

        if grid.type ~= GRID_TYPE_GARDEN then
            return false;
        end

        grid.garden_state = GARDEN_EMPTY;

        ME.user.bine = nil;
        ME.user.dbase:delete("bine");

        -- 立马保存
        DungeonActionM.immediatelySave();

        -- 抛出事件
        EventMgr.fire(event.GARDEN_RESET, {["pos"] = pos});

        return false;
    end,
};

